<head>
<meta charset="UTF-8">
<script src="jquery.js"></script>
<script type="text/javascript"></script>
</head>

<?php
$allFields = array("journal", "issue", "title", "titlenative", "abstract", "abstractnative", "keyword");
print("allFields : ");
print_r($allFields);
print("<br/><br/>");

// $dataCondition = array("title" => "a", "keyword" => "aa");
$dataCondition = array("title" => "");
print("dataCondition : ");
print_r($dataCondition);
print("<br/><br/>");

// $cursor = searhAllFields('article', $allFields, "title a");
// $cursor = searchMetaData("article", $dataCondition, "or");
$cursor = refineBy("article", $dataCondition, "refine", "title", "or");

function searhAllFields($collectionName, $allFields, $searchTxt) {
	// Declare Connection.
	$db = (new Mongo('mongodb://aci:aci1234@planet.thaimongo.com:27017/u160_aci'))->u160_aci;
	$collection = new MongoCollection($db, $collectionName);
	
	// Query
	$conditions = generateAllFieldsCondition($allFields, $searchTxt);
	$cursor = $collection->find($conditions);
	
	print('searhAllFields conditions :::::'.'<br/><br/>');
	print_r($conditions);
	print('<br/><br/>'.'searhAllFields result :::::'.'<br/><br/>');
	foreach ($cursor as $doc) {
		print_r($doc);
		print("<br/><br/>");
	}
	
	return $cursor;
}

function searchMetaData($collectionName, $dataCondition, $operation) {
	// Declare Connection.
	$db = (new Mongo('mongodb://aci:aci1234@planet.thaimongo.com:27017/u160_aci'))->u160_aci;
	$collection = new MongoCollection($db, $collectionName);
	
	// Query
	$conditions = generateConditions($dataCondition, $operation);	
	$cursor = $collection->find($conditions);
	
	print('searchMetaData conditions :::::'.'<br/><br/>');
	print_r($conditions);
	print('<br/><br/>'.'searchMetaData result :::::'.'<br/><br/>');
	foreach ($cursor as $doc) {
		print_r($doc);
		print("<br/><br/>");
	}
	
	return $cursor;
}

function refineBy($collectionName, $dataCondition, $groupName, $groupField, $operation) {
	// Declare Connection.
	$db = (new Mongo('mongodb://aci:aci1234@planet.thaimongo.com:27017/u160_aci'))->u160_aci;
	$collection = new MongoCollection($db, $collectionName);
	
	// Query
	$matchCondition = generateConditions($dataCondition, $operation);
	$conditions = array(
			array("\$match" => $matchCondition),
			array("\$group" => array(
					"_id" => array($groupName => "\$".$groupField),
					"count" => array("\$sum" => 1)
			)),
			array("\$sort" => array("count" => -1))
	);
	$cursor = $collection->aggregate($conditions);
	
	$result = array();
	foreach ($cursor as $doc) {
		$result[] = $doc;
		print_r($doc);
	}
	$result = $result[0];
	
	print('refineBy conditions :::::'.'<br/><br/>');
	print_r($conditions);
	print('<br/><br/>'.'refineBy result :::::'.'<br/><br/>');
	foreach ($result as $r) {
		print_r($r);
		print("<br/><br/>");
	}
	
	print('cursor :::::'.'<br/><br/>');
	print_r($cursor);
	print('result :::::'.'<br/><br/>');
	print_r($result);
	
	return $cursor;
}

function generateConditions($dataCondition, $operation) {
	// and, or
	$conditions = array();
	if ($operation == "and") {
		foreach ($dataCondition as $key => $value) {
			$conditions[$key] = new MongoRegex('/'.$value.'/i');
		}
	} else if ($operation == "or") {
		foreach ($dataCondition as $key => $value) {
			$conditions[] = array($key => new MongoRegex('/'.$value.'/i'));
		}
		$conditions = array("\$or" => $conditions);
	}
	return $conditions;
}

function generateAllFieldsCondition($allFields, $searchTxt) {
	// or
	$conditions = array();
	foreach ($allFields as $f) {
		$conditions[] = array($f => new MongoRegex('/'.$searchTxt.'/i'));
	}
	$conditions = array("\$or" => $conditions);
	return $conditions;
}
?>